/*
 * ClientSideHandler.java
 *
 * Created on July 25, 2007, 10:18 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.mobileandembedded.idesktop;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

import javax.microedition.lcdui.Image;


import com.mar.meclient.bt.BTMidlet;
import com.mar.meclient.bt.IBluetoothIOHandler;


/**
 *
 * @author helio
 */
public class ClientSideHandler {

    private RemoteDesktopCanvas remoteDesktopCanvas;
    private InputStream inputStream;
    private OutputStream outputStream;
    private Thread inputStreamListenerThread;

    /**
     * Creates a new instance of ClientSideHandler
     *
     * @param iDesktopClient
     * @param socketConnection
     * @throws java.io.IOException
     */
    public ClientSideHandler(IBluetoothIOHandler btIOHandler) throws IOException{
        this.inputStream = btIOHandler.getInputStream();
        this.outputStream = btIOHandler.getOutputStream();
        this.remoteDesktopCanvas = new RemoteDesktopCanvas( this);
        startInputStreamListenerThread();
    }
    
    public RemoteDesktopCanvas getRemoteDesktopCanvas() {
		return remoteDesktopCanvas;
	}
    /**
     *
     */
    private void startInputStreamListenerThread() {
        inputStreamListenerThread = new Thread(new Runnable() {

            public void run() {
                try {
//    				while (true){
//    					try {
//    						byte[] test = new byte[4];
//    						inputStream.read(test);
//    						IDesktopClient.getInstance().showAlert(">> "+new String(test));
//    						Thread.yield();
//    					} catch (IOException e) {
//    						IDesktopClient.getInstance().showAlert("!>> "+e.toString());
//    						break;
//    					}
//    					

                    while (true) {
                        byte[] message = IDesktopProtocol.readMessage(inputStream);
                        String operation = IDesktopProtocol.getOperation(message);
                        byte[] params = IDesktopProtocol.getParameters(message);
                        processMessage(operation, params);
                    }
    				
                } catch (Exception ex) {
                    ex.printStackTrace();
                    BTMidlet.getInstance().showAlert(ex);
                }
            }
        });

        inputStreamListenerThread.start();
    }

    /**
     *
     * @param params
     * @throws java.io.IOException
     */
    public void sendKeyboardInputMessage(String params) throws IOException {
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.KEYBOARD_INPUT_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @throws java.io.IOException
     */
    public void sendMouseLeftClickMessage() throws IOException {
        Vector params = new Vector();
        params.addElement(((remoteDesktopCanvas.getMouseX() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportX()) + "");
        params.addElement(((remoteDesktopCanvas.getMouseY() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportY()) + "");
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.MOUSE_LEFT_CLICK_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @throws java.io.IOException
     */
    public void sendMouseRightClickMessage() throws IOException {
        Vector params = new Vector();
        params.addElement(((remoteDesktopCanvas.getMouseX() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportX()) + "");
        params.addElement(((remoteDesktopCanvas.getMouseY() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportY()) + "");
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.MOUSE_RIGHT_CLICK_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @throws java.io.IOException
     */
    public void sendMouseLeftPressMessage() throws IOException {
        Vector params = new Vector();
        params.addElement(((remoteDesktopCanvas.getMouseX() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportX()) + "");
        params.addElement(((remoteDesktopCanvas.getMouseY() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportY()) + "");
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.MOUSE_LEFT_PRESS_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @throws java.io.IOException
     */
    public void sendMouseRightPressMessage() throws IOException {
        Vector params = new Vector();
        params.addElement(((remoteDesktopCanvas.getMouseX() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportX()) + "");
        params.addElement(((remoteDesktopCanvas.getMouseY() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportY()) + "");
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.MOUSE_RIGHT_PRESS_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @throws java.io.IOException
     */
    public void sendMouseLeftReleaseMessage() throws IOException {
        Vector params = new Vector();
        params.addElement(((remoteDesktopCanvas.getMouseX() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportX()) + "");
        params.addElement(((remoteDesktopCanvas.getMouseY() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportY()) + "");
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.MOUSE_LEFT_RELEASE_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @throws java.io.IOException
     */
    public void sendMouseRightReleaseMessage() throws IOException {
        Vector params = new Vector();
        params.addElement(((remoteDesktopCanvas.getMouseX() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportX()) + "");
        params.addElement(((remoteDesktopCanvas.getMouseY() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportY()) + "");
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.MOUSE_RIGHT_RELEASE_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @return
     */
    public void sendMoveMouseMessage() {
        try {
            Vector params = new Vector();
            params.addElement(((remoteDesktopCanvas.getMouseX() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportX()) + "");
            params.addElement(((remoteDesktopCanvas.getMouseY() * remoteDesktopCanvas.getZoom()) + remoteDesktopCanvas.getViewportY()) + "");
            byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.MOUSE_MOVE_MESSAGE, params);
            sendMessage(message);
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }

    /**
     *
     * @throws java.io.IOException
     */
    public void sendDeviceConfigurationMessage() throws IOException {
        Vector params = new Vector();
        params.addElement(remoteDesktopCanvas.getWidth() + "");
        params.addElement(remoteDesktopCanvas.getHeight() + "");
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.DEVICE_CONFIGURATION_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @throws java.io.IOException
     */
    public void sendRequestScreenshotMessage() throws IOException {
        Vector params = new Vector();
        params.addElement(remoteDesktopCanvas.getViewportX() + "");
        params.addElement(remoteDesktopCanvas.getViewportY() + "");
        params.addElement(remoteDesktopCanvas.getViewportWidth() + "");
        params.addElement(remoteDesktopCanvas.getViewportHeight() + "");
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.REQUEST_SCREENSHOT_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @param email
     * @param password
     * @throws java.io.IOException
     */
    public void sendAuthenticationMessage(String email, String password) throws IOException {
        Vector params = new Vector();
        params.addElement(email);
        params.addElement(password);
        byte[] message = IDesktopProtocol.createMessage(IDesktopProtocol.AUTHENTICATION_MESSAGE, params);
        sendMessage(message);
    }

    /**
     *
     * @param params
     */
    private void receiveScreenshot(byte[] params) {
        try {
            Image image = Image.createImage(params, 0, params.length);
            remoteDesktopCanvas.setDesktopImage(image);
        } catch (Exception ex) {
            ex.printStackTrace();
            BTMidlet.getInstance().showAlert(ex);
        }
    }

    /**
     * @param operation
     * @param params
     * @throws java.io.IOException
     */
    public void processMessage(String operation, byte[] params) throws IOException {
        if (operation.equals(IDesktopProtocol.SCREENSHOT_MESSAGE)) {
            receiveScreenshot(params);
        } else if (operation.equals(IDesktopProtocol.AUTHENTICATION_FAILED_MESSAGE)) {
        	BTMidlet.getInstance().showAlert(new String(params));
        } else if (operation.equals(IDesktopProtocol.AUTHENTICATION_SUCCESSFUL_MESSAGE)) {
        	BTMidlet.getInstance().showAlert("Auth passed!");
        }
    }

    /**
     *
     * @param message
     * @throws java.io.IOException
     */
    public void sendMessage(byte[] message) throws IOException {
        outputStream.write(message);
        outputStream.flush();
    }


//    /**
//     *
//     * @param remoteDesktopCanvas
//     */
//    public void setRemoteDesktopCanvas(RemoteDesktopCanvas remoteDesktopCanvas) {
//        this.remoteDesktopCanvas = remoteDesktopCanvas;
//    }
}
